using System;
using NUnit.Framework;
using SharpTestsEx;

namespace AltNetHispano.Vale.Tests.Example
{
	public class PersonServiceTest
	{
		private IValidator validator;
		[TestFixtureSetUp]
		public void ConfigureValidator()
		{
			validator = new Validator();
			validator.Register<Person>(p => validator.StringIsRequired("Name", p.Name));
			validator.Register<Person>(p => validator.StringIsRequired("Surname", p.Surname));
			validator.Register<Person>(p => validator.DateIsInThePast("Birthday", p.Birthday));
			validator.Register<Person>(p => validator.StringLen("Name", p.Name, 20));
			validator.Register<Person>(p => validator.StringLen("Surname", p.Surname, 30));

		}
		[Test]
		public void WhenInvalidWithDefaultValuesThenReturnInvalidMessagges()
		{
			var service = new PersonCrudAppService(validator);
			var person = new Person{ Name = null, Surname = null, Birthday = DateTime.Today};
			var invalidMessages = service.Save(person);
			invalidMessages.Should().Contain("Name is required.");
			invalidMessages.Should().Contain("Surname is required.");
			invalidMessages.Should().Contain("Birthday should be in the past.");
		}

		[Test]
		public void WhenNameIsTooLongThenReturnInvalidMessaggeForName()
		{
			var service = new PersonCrudAppService(validator);
			var person = new Person { Name = "0123456789012345678901", Surname = null, Birthday = DateTime.Today };
			var invalidMessages = service.Save(person);
			invalidMessages.Should().Contain("Name is too long.");
		}

		[Test]
		public void WhenSurnameIsTooLongThenReturnInvalidMessaggeForSurname()
		{
			var service = new PersonCrudAppService(validator);
			var person = new Person { Name = null, Surname = "0123456789012345678901234567890123", Birthday = DateTime.Today };
			var invalidMessages = service.Save(person);
			invalidMessages.Should().Contain("Surname is too long.");
		}

		[Test]
		public void WhenInvalidWithEmptyNameAndSurnameThenReturnInvalidMessagges()
		{
			var service = new PersonCrudAppService(validator);
			var person = new Person { Name = "", Surname = ""};
			var invalidMessages = service.Save(person);
			invalidMessages.Should().Contain("Name is required.");
			invalidMessages.Should().Contain("Surname is required.");
		}

		[Test]
		public void WhenOkThenReturnEmptyInvalidMessagges()
		{
			var service = new PersonCrudAppService(validator);
			var person = new Person { Name = "0123456789", Surname = "0123456789", Birthday = DateTime.Today.AddDays(-20) };
			var invalidMessages = service.Save(person);
			invalidMessages.Should().Be.Empty();
		}
	}
}